from django.shortcuts import render
from django.http.response import HttpResponse
from django.views.decorators.http import require_http_methods

import os
import json

# Create your views here.
@require_http_methods(["GET", "POST"])
def index(request):
    username = request.GET.get('username')
    userage = request.GET.get('userage', 0)
    print(username)
    print(userage)
    #获取列表
    usernamelist = request.GET.getlist('username')
    print(usernamelist)
    if username:
        data = "<h1>Hello,"+username+"!</h1>"
    else:
        data = "<h1>Hello,World!</h1>"
    return HttpResponse(data, content_type="text/html")
# Create your views here.

@require_http_methods(["GET", "POST"])
def index2(request):
    print("------index2-------")
    print(request.POST)

    print(request.POST.get('username'))
    print(request.POST.get('age'))
    print(request.POST.getlist('lve'))

    print(request.GET.get('val'))

    #body  raw json方式入参
    print(request.body)
    
    body = json.loads(request.body)
    print(body)
    print(body.get('username'))

    return HttpResponse("Hello, World!")

def index3(request):
    print(request.META)
    print(request.META.get('CONTENT_TYPE'))
    print(request.META.get('REQUEST_METHOD'))
    print(request.META.get('HTTP_ROLE'))
    print(request.method)
    print(request.headers)
    print(request.headers.get('Content-Type'))
    print(request.headers.get('Role'))

    return HttpResponse("Hello, World!")


def index4(request):
    #上传文件
    # print(f"{os.path.dirname(__file__)}")
    for file in request.FILES.getlist('upfile'):
        with open(f"{os.path.dirname(__file__)}/{file.name}", 'wb+') as f:
            f.write(file.read())

    return HttpResponse("Hello, World!")